package com.kele.service.impl;

import com.kele.service.LoginService;
import com.kele.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;
import java.util.UUID;

/**
 * @author chunhui.li
 * @date 2021/10/18 14:52
 * @Description
 * @ClassName LoginServiceImpl
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    RedisUtil redisUtil;

    @Override
    public String login(String username, String password) {

        if (Objects.equals("kele",username)&&Objects.equals("123456",password)){
            String token= UUID.randomUUID().toString();


            redisUtil.set(token,username);
            return "用户: " + username + "登录成功，token是: " + token;
        }else {
            return "用户名或密码错误，登录失败！";
        }

    }

    @Override
    public String logout(HttpServletRequest request) {
        Boolean delete=null;
        String token=request.getHeader("token");
        if (token!=null&&token!=""){
            delete= redisUtil.delete(token);
        }
        if (!delete) {
            return "注销失败，请检查是否登录！";
        }
        return "注销成功！";
    }
}
